package com.hdyg.sixg.activity.trans;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.PendOrderBean;
import com.hdyg.sixg.mvp.Contrant.CPendOrder;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PPendOrder;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 挂单
 * @time 2019/4/8 14:45
 */
public class PendOrderActivity extends BaseActivity implements CPendOrder.IVPendOrder {
    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_limit_number)
    SuperTextView svLimitNumber;
    @BindView(R.id.et_buy_number)
    EditText etBuyNumber;
    @BindView(R.id.sv_valuation)
    SuperTextView svValuation;
    @BindView(R.id.et_single_price)
    EditText etSinglePrice;
    @BindView(R.id.sv_all_valuation)
    SuperTextView svAllValuation;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CPendOrder.IPPendOrder mPresenter;
    private PendOrderBean dataBean;
    private String sellamount, single_price, trapass;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pend_order;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.pend_title);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        etBuyNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sellamount = s.toString();
                if (!TextUtils.isEmpty(sellamount)){
                    setAllValuation();
                }
            }
        });
        etSinglePrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                single_price = s.toString();
                if (!TextUtils.isEmpty(single_price)){
                    setAllValuation();
                }
            }
        });
        //确认挂售
        btSure.setOnClickListener(v -> {
            sellamount = etBuyNumber.getText().toString().trim();
            single_price = etSinglePrice.getText().toString().trim();
            trapass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(sellamount)) {
                toastNotifyShort(R.string.pend_pend_number_hint);
                return;
            }
            if (TextUtils.isEmpty(single_price)) {
                toastNotifyShort(R.string.trans_valuation_hint);
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyPend(BaseUrl.SELL_HANG_UP_URL, GetParamUtil.getSellHandUpParam(sellamount, single_price, trapass));
        });
    }
    //设置总估值
    private void setAllValuation(){
        if (TextUtils.isEmpty(sellamount)){
            sellamount = "0";
        }
        if (TextUtils.isEmpty(single_price)){
            single_price = "0";
        }
        double str = Double.valueOf(sellamount) * Double.valueOf(single_price);
        svAllValuation.setRightString(StringUtil.dealFloatStr(str));
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.HANG_UP_URL, GetParamUtil.getHandUpParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PPendOrder(this);
    }

    @Override
    public void vGetInitSuccess(PendOrderBean dataBean) {
        this.dataBean = dataBean;
        svLimitNumber.setRightString(dataBean.getMoney());
        svValuation.setRightString(dataBean.getFee());
    }

    @Override
    public void vGetApplyPendSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }
}
